package net.adst.beans;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 05.11.12
 * Time: 9:57
 * To change this template use File | Settings | File Templates.
 */

import org.apache.poi.hssf.usermodel.HSSFRow;

import java.text.SimpleDateFormat;
import java.util.Date;

public class OurTecRow {
    private Date date;
    private String chIN;
    private String chOut;
    private String sureName;
    private String hoursFacts;
    private String hours;
    private String priceForHour;
    private String profit;
    private String profitFact;
    private String kindOfPayment;
    private String customer;
    private String objectName;
    private String dtAmount;
    private String dtCost;
    private String salaryInRub;
    private String dopTraty;
    private String pOtchet;
    private String income;
    private String prorab;
    private String comment;

    private String tecName;
    private int rowInFile;

    public OurTecRow(HSSFRow row, String tecName) {
        this.tecName = tecName;
        if (row.getCell(0).toString() != null) {
            this.date = row.getCell(0).getDateCellValue();
        } else {
            this.date = new Date();
        }
        if (row.getCell(1).toString() != null) {
            this.chIN = row.getCell(1).toString();
        } else {
            this.chIN = "";
        }
        if (row.getCell(2).toString() != null) {
            this.chOut = row.getCell(2).toString();
        } else {
            this.chOut = "";
        }

        if (row.getCell(3).toString() != null) {
            this.sureName = row.getCell(3).toString();
        } else {
            this.sureName = "";
        }
        if (row.getCell(4).toString() != null) {
            this.hoursFacts = row.getCell(4).toString();
        } else {
            this.hoursFacts = "";
        }
        if (row.getCell(5).toString() != null) {
            this.hours = row.getCell(5).toString();
        } else {
            this.hours = "";
        }
        if ((row.getCell(4).toString() != null) && (row.getCell(6).toString() != null) && (!row.getCell(4).toString().isEmpty()) && (!row.getCell(6).toString().isEmpty())) {
            Double priceHour = new Double(row.getCell(6).toString());
            Double hour = new Double(row.getCell(4).toString());
            this.profitFact = String.valueOf(priceHour * hour);
        } else {
            this.profitFact = "";
        }

        if ((row.getCell(5).toString() != null) && (row.getCell(6).toString() != null) && (!row.getCell(5).toString().isEmpty()) && (!row.getCell(6).toString().isEmpty())) {
            Double priceHour = new Double(row.getCell(6).toString());
            Double hour = new Double(row.getCell(5).toString());
            this.profit = String.valueOf(priceHour * hour);
        } else {
            this.profit = "";
        }

        if (row.getCell(9).toString() != null) {
            this.kindOfPayment = row.getCell(9).toString();
        } else {
            this.kindOfPayment = "";
        }

        if (row.getCell(10).toString() != null) {
            this.customer = row.getCell(10).toString();
        } else {
            this.customer = "";
        }

        if (row.getCell(11).toString() != null) {
            this.objectName = row.getCell(11).toString();
        } else {
            this.objectName = "";
        }

        if (row.getCell(12).toString() != null) {
            this.dtAmount = row.getCell(12).toString();
        } else {
            this.dtAmount = "";
        }

        if (row.getCell(13).toString() != null) {
            this.dtCost = row.getCell(13).toString();
        } else {
            this.dtCost = "";
        }

        if (row.getCell(14).toString() != null) {
            this.salaryInRub = row.getCell(14).toString();
        } else {
            this.salaryInRub = "";
        }

        if (row.getCell(15).toString() != null) {
            this.dopTraty = row.getCell(15).toString();
        } else {
            this.dopTraty = "";
        }

        if (row.getCell(16).toString() != null) {
            this.pOtchet = row.getCell(16).toString();
        } else {
            this.pOtchet = "";
        }

        if (row.getCell(18).toString() != null) {
            this.prorab = row.getCell(18).toString();
        } else {
            this.prorab = "";
        }

        if ((row.getCell(17).toString() != null)) {
            Double profitFact = this.profitFact.isEmpty() ? 0 : Double.valueOf(this.profitFact);
            Double dtCost = this.dtCost.isEmpty() ? 0 : Double.valueOf(this.dtCost);
            Double salaryInRub = this.salaryInRub.isEmpty() ? 0 : Double.valueOf(this.salaryInRub);
            Double dopTraty = this.dopTraty.isEmpty() ? 0 : Double.valueOf(this.dopTraty);
            Double prorab = this.prorab.isEmpty() ? 0 : Double.valueOf(this.prorab);
            double nal = 0.94;
            this.income = String.valueOf(profitFact * nal - dtCost - salaryInRub - dopTraty - prorab);
        } else {
            this.income = "";
        }

        if (row.getCell(19) != null || row.getCell(20) != null || row.getCell(21) != null || row.getCell(22) != null || row.getCell(23) != null || row.getCell(24) != null || row.getCell(25) != null || row.getCell(26) != null || row.getCell(27) != null) {
            this.comment = row.getCell(19) != null ? row.getCell(19).toString() : "";
            for (int i = 20; i < 27; i++) {
                if (row.getCell(i) != null && !row.getCell(i).toString().equals("")) {
                    this.comment = this.comment.equals("") ? this.comment : this.comment + "\n" + row.getCell(i).toString();
                }
            }
            if (this.comment == null) {
                this.comment = "";
            }
        } else {
            this.comment = "";
        }

        this.rowInFile = row.getRowNum();
    }

    public OurTecRow(Date date, String chIN, String chOut, String sureName, String hoursFacts, String hours, String priceForHour, String profit, String profitFact, String kindOfPayment, String customer, String objectName, String dtAmount, String dtCost, String salaryInRub, String dopTraty, String pOtchet, String income, String prorab, String comment) {
        this.date = date;
        this.chIN = chIN;
        this.chOut = chOut;
        this.sureName = sureName;
        this.hoursFacts = hoursFacts;
        this.hours = hours;
        this.priceForHour = priceForHour;
        this.profit = profit;
        this.profitFact = profitFact;
        this.kindOfPayment = kindOfPayment;
        this.customer = customer;
        this.objectName = objectName;
        this.dtAmount = dtAmount;
        this.dtCost = dtCost;
        this.salaryInRub = salaryInRub;
        this.dopTraty = dopTraty;
        this.pOtchet = pOtchet;
        this.income = income;
        this.prorab = prorab;
        this.comment = comment;
    }

    public String getTecName() {
        return tecName;
    }

    public void setTecName(String tecName) {
        this.tecName = tecName;
    }

    public int getRowInFile() {
        return rowInFile;
    }

    public void setRowInFile(int rowInFile) {
        this.rowInFile = rowInFile;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getChIN() {
        return chIN;
    }

    public void setChIN(String chIN) {
        this.chIN = chIN;
    }

    public String getChOut() {
        return chOut;
    }

    public void setChOut(String chOut) {
        this.chOut = chOut;
    }

    public String getSureName() {
        return sureName;
    }

    public void setSureName(String sureName) {
        this.sureName = sureName;
    }

    public String getHoursFacts() {
        return hoursFacts;
    }

    public void setHoursFacts(String hoursFacts) {
        this.hoursFacts = hoursFacts;
    }

    public String getHours() {
        return hours;
    }

    public void setHours(String hours) {
        this.hours = hours;
    }

    public String getPriceForHour() {
        return priceForHour;
    }

    public void setPriceForHour(String priceForHour) {
        this.priceForHour = priceForHour;
    }

    public String getProfit() {
        return profit;
    }

    public void setProfit(String profit) {
        this.profit = profit;
    }

    public String getProfitFact() {
        return profitFact;
    }

    public void setProfitFact(String profitFact) {
        this.profitFact = profitFact;
    }

    public String getKindOfPayment() {
        return kindOfPayment;
    }

    public void setKindOfPayment(String kindOfPayment) {
        this.kindOfPayment = kindOfPayment;
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public String getObjectName() {
        return objectName;
    }

    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }

    public String getDtAmount() {
        return dtAmount;
    }

    public void setDtAmount(String dtAmount) {
        this.dtAmount = dtAmount;
    }

    public String getDtCost() {
        return dtCost;
    }

    public void setDtCost(String dtCost) {
        this.dtCost = dtCost;
    }

    public String getSalaryInRub() {
        return salaryInRub;
    }

    public void setSalaryInRub(String salaryInRub) {
        this.salaryInRub = salaryInRub;
    }

    public String getDopTraty() {
        return dopTraty;
    }

    public void setDopTraty(String dopTraty) {
        this.dopTraty = dopTraty;
    }

    public String getpOtchet() {
        return pOtchet;
    }

    public void setpOtchet(String pOtchet) {
        this.pOtchet = pOtchet;
    }

    public String getIncome() {
        return income;
    }

    public void setIncome(String income) {
        this.income = income;
    }

    public String getProrab() {
        return prorab;
    }

    public void setProrab(String prorab) {
        this.prorab = prorab;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public boolean isEmpty() {
        if (!chIN.isEmpty() || !chOut.isEmpty() || !sureName.isEmpty() || !kindOfPayment.isEmpty() || (!hoursFacts.isEmpty() && !hoursFacts.equals("-")) || (!hours.isEmpty() && !hours.equals("-")) || (!profitFact.isEmpty() && !profitFact.equals("0.0")) || (!priceForHour.isEmpty() && !priceForHour.equals("0.0")) || !customer.isEmpty() || (!profit.isEmpty() && !profit.equals("0.0")) || !objectName.isEmpty() || (!income.isEmpty() && !income.equals("0.0")) || !objectName.isEmpty() || (!dtAmount.isEmpty()) || (!profit.isEmpty()) || !dtCost.isEmpty() || !prorab.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public Object[] toRow() {
        return new Object[]{(new SimpleDateFormat("dd.MM.yyyy")).format(date.getTime()), chIN, chOut, sureName, hoursFacts, hours, profit, profitFact, kindOfPayment, customer, objectName, dtAmount, dtCost, salaryInRub, dopTraty, income, prorab, comment.equals("") ? "" : "*"};
    }

}
